package com.bayihouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bayihouse.constant.CommonConstant;
import com.bayihouse.mapper.RoomMapper;
import com.bayihouse.model.entity.Room;
import com.bayihouse.model.request.room.RoomQueryRequest;
import com.bayihouse.model.vo.RoomVO;
import com.bayihouse.service.RoomService;
import com.bayihouse.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【room】的数据库操作Service实现
 * @createDate 2024-09-17 10:28:03
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
        implements RoomService {

    @Override
    public RoomVO getRoomVO(Room room) {
        if (room == null) {
            return null;
        }
        return RoomVO.objToVO(room);
    }

    @Override
    public QueryWrapper<Room> getQueryWrapper(RoomQueryRequest roomQueryRequest) {
        Long id = roomQueryRequest.getId();
        Integer category = roomQueryRequest.getCategory();
        Integer height = roomQueryRequest.getHeight();
        List<String> label = roomQueryRequest.getLabel();
        Long unitId = roomQueryRequest.getUnitId();
        Long communityId = roomQueryRequest.getCommunityId();
        String detail = roomQueryRequest.getDetail();
        Long householdId = roomQueryRequest.getHouseholdId();
        String name = roomQueryRequest.getName();
        String sortField = roomQueryRequest.getSortField();
        String sortOrder = roomQueryRequest.getSortOrder();
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        if (CollectionUtils.isNotEmpty(label)) {
            for (String tag : label) {
                queryWrapper.like("tags", "\"" + tag + "\"");
            }
        }
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(category != null, "category", category);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.eq(height != null, "height", height);
        queryWrapper.eq(unitId != null, "unitId", unitId);
        queryWrapper.eq(communityId != null, "communityId", communityId);
        queryWrapper.eq(StringUtils.isNotBlank(detail), "detail", detail);
        queryWrapper.eq(householdId != null, "householdId", householdId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public List<RoomVO> getRoomVOList(List<Room> records) {
        if (CollectionUtils.isEmpty(records)) {
            return new ArrayList<>();
        }
        return records.stream().map(this::getRoomVO).collect(Collectors.toList());
    }

    @Override
    public Long getByUnitIdAndAddress(Long unitId, String address, String roomType) {
        if (StringUtils.isNotBlank(address)) {
            QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("unitId", unitId);
            queryWrapper.like("name", address);
            queryWrapper.or(o -> o.like("category", roomType));
            Room room = this.getOne(queryWrapper);
            return room.getId();
        }
        return null;
    }

    @Override
    public Long getComunityIdAndAddress(Long communityId, String address, String roomType) {
        if (StringUtils.isNotBlank(address)) {
            QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("communityId", communityId);
            queryWrapper.like("name", address);
            queryWrapper.or(o -> o.like("category", roomType));
            Room room = this.getOne(queryWrapper);
            return room.getId();
        }
        return null;
    }
}




